package com.cskaoyan.mall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.common.constant.RedisConst;
import com.cskaoyan.mall.product.client.SearchApiClient;
import com.cskaoyan.mall.product.dto.*;
import com.cskaoyan.mall.product.service.CategoryService;
import com.cskaoyan.mall.product.service.ProductDetailService;
import com.cskaoyan.mall.product.service.SkuService;
import com.cskaoyan.mall.product.service.SpuService;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 商品详情页
 *
 * @author Macro_Ray
 * @since 2024/6/12 下午9:46
 */
@Service
public class ProductDetailServiceImpl implements ProductDetailService {

    @Autowired
    SkuService skuService;
    @Autowired
    SpuService spuService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    SearchApiClient searchApiClient;

    @Override
    public ProductDetailDTO getPDBySkuId(Long skuId) {
        // 需要返回的商品详情DTO
        ProductDetailDTO productDetailDTO = new ProductDetailDTO();

        // 利用布隆过滤器判断元素是否不存在于 skuId 集合中
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(skuId)) {
            return productDetailDTO;
        }

        // 创建异步任务
        CompletableFuture<SkuInfoDTO> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            // skuInfo - 商品详情
            SkuInfoDTO skuInfo = skuService.getSkuInfo(skuId);
            productDetailDTO.setSkuInfo(skuInfo);
            return skuInfo;
        });

        CompletableFuture<Void> cateHierarchyCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // categoryHierarchy - 商品分类层级
            CategoryHierarchyDTO cateHierarchy = categoryService.getCateHierarchy(skuInfo.getThirdLevelCategoryId());
            productDetailDTO.setCategoryHierarchy(cateHierarchy);
        });

        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // spuSaleAttrList - 商品销售属性
            List<SpuSaleAttrInfoDTO> spuSaleAttrListCheckBySku = skuService.getSpuSaleAttrListCheckBySku(skuId,
                    skuInfo.getSpuId());
            productDetailDTO.setSpuSaleAttrList(spuSaleAttrListCheckBySku);
        });

        CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // spuPosterList - 商品海报
            List<SpuPosterDTO> spuPoster = spuService.getSpuPosterBySpu(skuInfo.getSpuId());
            productDetailDTO.setSpuPosterList(spuPoster);
        });

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            // valuesSkuJson - spu中包含的不同sku商品的销售属性值组合
            Map<String, Long> skuValueIdsMap = skuService.getSkuValueIdsMap(skuInfo.getSpuId());
            String valueIdsJson = JSON.toJSONString(skuValueIdsMap);
            productDetailDTO.setValuesSkuJson(valueIdsJson);
        });

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            // price - 商品价格
            BigDecimal skuPrice = skuService.getSkuPrice(skuId);
            productDetailDTO.setPrice(skuPrice);
        });

        CompletableFuture<Void> skuAttrListCompletableFuture = CompletableFuture.runAsync(() -> {
            // skuAttrList - 商品规格与包装
            List<SkuSpecificationDTO> skuSpecificationList = new ArrayList<>();
            List<PlatformAttrInfoDTO> platformAttrInfoBySku = skuService.getPlatformAttrInfoBySku(skuId);
            for (PlatformAttrInfoDTO platformAttrInfo : platformAttrInfoBySku) {
                SkuSpecificationDTO skuSpecificationDTO = new SkuSpecificationDTO();
                skuSpecificationDTO.setAttrName(platformAttrInfo.getAttrName());
                skuSpecificationDTO.setAttrValue(platformAttrInfo.getAttrValueList().get(0).getValueName());
                skuSpecificationList.add(skuSpecificationDTO);
            }
            productDetailDTO.setSkuAttrList(skuSpecificationList);
        });

        // 增加一个异步任务，调用搜索服务增加该商品的热度
        CompletableFuture.runAsync(() -> {
            // 搜索服务的调用
            searchApiClient.incrHotScore(skuId);
        });

        // 创建多元依赖
        CompletableFuture<Void> allCompletableFuture = CompletableFuture.allOf(cateHierarchyCompletableFuture,
                spuSaleAttrCompletableFuture, spuPosterCompletableFuture,
                valuesSkuJsonCompletableFuture, priceCompletableFuture, skuAttrListCompletableFuture);
        // 等待所有异步任务完成
        allCompletableFuture.join();

        return productDetailDTO;
    }
}
